from django.db import models
from django.utils import timezone
# from django.contrib.auth.models import User


def get_goods_upload_to(instance, filename):
    """
    A simple way to return the full path
    room_image
    background_image
    finished_product
    """
    timenow = timezone.now()
    rpath = 'goods_image/' + instance.uploadUser.username + '/' + str(timenow.year) + '-' \
            + str(timenow.month) + '/' + filename
    return rpath


# Create your models here.
class Goods(models.Model):
    goodsTitle = models.CharField(max_length=32)
    goodsPrice = models.CharField(max_length=32)
    goodsSrc = models.ImageField(upload_to=get_goods_upload_to)
    uploadTime = models.DateTimeField(auto_now_add=True)
    lastChangeTime = models.DateTimeField(auto_now=True)
    uploadUser = models.ForeignKey(to='userprofile.BusinessInfo', on_delete=models.CASCADE, to_field='user_ptr_id')
    description = models.CharField(max_length=300, blank=True, default='')

    def __str__(self):
        return self.goodsTitle

    class Meta:
        verbose_name = '商品表'
        verbose_name_plural = '商品表'
        ordering = ['uploadTime']


class Comments(models.Model):
    goods = models.ForeignKey(to='Goods', on_delete=models.CASCADE, related_name='conments')
    user = models.ForeignKey(to='userprofile.UserInfo', on_delete=models.CASCADE, related_name='conments')
    # 评论内容
    body = models.TextField()
    # 评论时间
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return self.body


class Contact(models.Model):
    goods = models.ForeignKey(to='Goods', on_delete=models.CASCADE, related_name='contacts')
    user = models.ForeignKey(to='userprofile.UserInfo', on_delete=models.CASCADE, related_name='contacts')
    businessuser = models.ForeignKey(to='userprofile.BusinessInfo', on_delete=models.CASCADE, related_name='contacts')
    # 联系手机号
    cphone = models.TextField()
    is_viewd = models.BooleanField(default=False)
    # 联系时间
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return self.cphone
